// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_CODE_RULE
// * CreateDate    : 2013-08-28 11:00:56
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:56
// * LastChanger   : 2013-08-28 11:00:56
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Mst
{
/// <summary>
    /// MstCodeRule
    /// </summary>
    public partial class MstCodeRuleBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstCodeRuleEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstCodeRuleEntity GetMstCodeRule(MstCodeRuleEntity objMstCodeRuleEntity)
        {
            return DataAccess.SelectSingle<MstCodeRuleEntity>(objMstCodeRuleEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstCodeRuleEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstCodeRule(MstCodeRuleEntity objMstCodeRuleEntity)
        {
            return DataAccess.Insert(objMstCodeRuleEntity);
        }

        public static bool InsertMstCodeRule(List<MstCodeRuleEntity> objMstCodeRuleEntityList)
        {
            return DataAccess.Insert(objMstCodeRuleEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstCodeRuleEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstCodeRule(MstCodeRuleEntity objMstCodeRuleEntity)
        {
            return DataAccess.Update(objMstCodeRuleEntity);
        }

        public static bool UpdateMstCodeRule(List<MstCodeRuleEntity> objMstCodeRuleEntityList)
        {
            return DataAccess.Update<MstCodeRuleEntity>(objMstCodeRuleEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstCodeRuleEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstCodeRule(MstCodeRuleEntity objMstCodeRuleEntity)
        {
            return DataAccess.Delete(objMstCodeRuleEntity);
        }

        public static bool DeleteMstCodeRule(List<MstCodeRuleEntity> objMstCodeRuleEntityList)
        {
            return DataAccess.Delete<MstCodeRuleEntity>(objMstCodeRuleEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstCodeRuleAll()
        {
            return DataAccess.SelectAllDataSet<MstCodeRuleEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstCodeRuleEntity> GetMstCodeRuleAllList()
        {
            return DataAccess.SelectAll<MstCodeRuleEntity>();
        }
        #endregion


        #region Functions
        public static MstCodeRuleEntity GetMstCodeRuleByCodeType(string codeType)
        {
            if (string.IsNullOrEmpty(codeType))
            {
                return null;
            }
            MstCodeRuleEntity entity = new MstCodeRuleEntity();
            entity.CodeType = codeType;
            return DataAccess.SelectSingle<MstCodeRuleEntity>(entity);
        }

        public static bool GetCurrentOrderCode(string codeType, out string code)
        {
            code = "";
            if (string.IsNullOrEmpty(codeType))
            {
                return false;
            }
            MstCodeRuleEntity entity = MstCodeRuleBs.GetMstCodeRuleByCodeType(codeType);
            if (entity == null)
            {
                return false;
            }
            char paddingChar = Convert.ToChar("0");
            string codeDateFormat = "yyyyMMdd";
            if (string.IsNullOrEmpty(entity.CodeDateFormat) == false)
            {
                codeDateFormat = entity.CodeDateFormat;
            }
            if (entity.CurrentDay == DateTime.Now.ToString(codeDateFormat))
            {
                entity.CurrentValue++;
            }
            else
            {
                entity.CurrentDay = DateTime.Now.ToString(codeDateFormat);
                entity.CurrentValue = 1;
            }
            string currentValue = entity.CurrentValue.ToString().PadLeft(entity.CodeSeqLen, paddingChar);
            code = entity.CodePrefix + DateTime.Now.ToString(codeDateFormat) + currentValue;
            DataAccess.Update(entity);
            return true;
        }

        public static string GetCurrentOrderCode(string codeType)
        {
            if (string.IsNullOrEmpty(codeType))
            {
                return null;
            }
            MstCodeRuleEntity entity = MstCodeRuleBs.GetMstCodeRuleByCodeType(codeType);
            if (entity == null)
            {
                return null;
            }
            char paddingChar = Convert.ToChar("0");
            string codeDateFormat = "yyyyMMdd";
            if (string.IsNullOrEmpty(entity.CodeDateFormat) == false)
            {
                codeDateFormat = entity.CodeDateFormat;
            }
            if (entity.CurrentDay == DateTime.Now.ToString(codeDateFormat))
            {
                entity.CurrentValue++;
            }
            else
            {
                entity.CurrentDay = DateTime.Now.ToString(codeDateFormat);
                entity.CurrentValue = 1;
            }
            string currentValue = entity.CurrentValue.ToString().PadLeft(entity.CodeSeqLen, paddingChar);            
            DataAccess.Update(entity);
            return entity.CodePrefix + DateTime.Now.ToString(codeDateFormat) + currentValue;
        }

        public static bool GetCurrentOrderCode(string codeType, out string code, DataAccessBroker broker)
        {
            code = "";
            if (string.IsNullOrEmpty(codeType))
            {
                return false;
            }
            MstCodeRuleEntity entity = MstCodeRuleBs.GetMstCodeRuleByCodeType(codeType);
            if (entity == null)
            {
                return false;
            }
            char paddingChar = Convert.ToChar("0");
            string codeDateFormat = "yyyyMMdd";
            if (string.IsNullOrEmpty(entity.CodeDateFormat) == false)
            {
                codeDateFormat = entity.CodeDateFormat;
            }
            if (entity.CurrentDay == DateTime.Now.ToString(codeDateFormat))
            {
                entity.CurrentValue++;
            }
            else
            {
                entity.CurrentDay = DateTime.Now.ToString(codeDateFormat);
                entity.CurrentValue = 1;
            }
            string currentValue = entity.CurrentValue.ToString().PadLeft(entity.CodeSeqLen, paddingChar);
            code = entity.CodePrefix + DateTime.Now.ToString(codeDateFormat) + currentValue;
            DataAccess.Update(entity, broker);
            return true;
        }

        #endregion
    }
	
	
}	

